<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统中心</el-breadcrumb-item>
            <el-breadcrumb-item>销售管理</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card class="box-card">

            <el-button  type="primary" @click="dialogVisible = true">添加销售</el-button>

            <el-table
                    :data="sellerList"
                    style="width: 100%">
                <el-table-column
                        type="index"
                        width="50">
                </el-table-column>
                <el-table-column
                        prop="userName"
                        label="用户名"
                        width="130">
                </el-table-column>
                <el-table-column
                        prop="nickName"
                        label="销售员名字"
                        width="140">
                </el-table-column>
                <el-table-column
                        prop="mobile"
                        label="联系电话"
                        width="140">
                </el-table-column>
                <el-table-column
                        prop="sex"
                        label="性别"
                        width="90">
                    <template slot-scope="scope">
                        {{scope.row.sex|sexFit}}
                    </template>
                </el-table-column>
                <el-table-column
                        prop="age"
                        label="年龄"
                        width="100">
                </el-table-column>
                <el-table-column
                        prop="saleFee"
                        label="收费标准"
                        width="100">
                </el-table-column>
                <el-table-column
                        prop="address"
                        label="地址"
                        width="140">
                </el-table-column>
                <el-table-column
                        label="入职时间"
                        width="140">
                    <template slot-scope="scope">
                        {{scope.row.joinTime|joinTimeFit}}
                    </template>
                </el-table-column>
                <el-table-column
                        prop="status"
                        label="销售状态"
                        width="100">
                    <template slot-scope="scope">
                        {{scope.row.status|statusFit}}
                    </template>
                </el-table-column>
                <el-table-column
                        label="操作"
                        width="320">
                    <!--点击按钮 需要绑定@click的时间 例如 @click="add"-->
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="editById(scope.row.id)">修改</el-button>
                        <el-button type="primary" size="mini" @click="delById(scope.row.id)">删除</el-button>
                        <el-button type="primary" size="mini" @click="openEditSellerPass(scope.row.id)">修改销售密码</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="block">
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="page.pageCurrent"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="page.pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </div>
        </el-card>
        <el-dialog
                title="添加销售"
                :visible.sync="dialogVisible"
                width="30%">
            <el-form ref="form" :model="sellerForm" label-width="80px" >
                <el-form-item label="登录账号">
                    <el-input v-model="sellerForm.userName"></el-input>
                </el-form-item>
                <el-form-item label="销售名称">
                    <el-input v-model="sellerForm.nickName"></el-input>
                </el-form-item>
                <el-form-item label="销售电话">
                    <el-input v-model="sellerForm.mobile"></el-input>
                </el-form-item>
                <el-form-item label="性别">
                    <el-radio v-model="sellerForm.sex" label="0">男</el-radio>
                    <el-radio v-model="sellerForm.sex" label="1">女</el-radio>
                </el-form-item>
                <el-form-item label="年龄">
                    <el-input v-model="sellerForm.age"></el-input>
                </el-form-item>
                <el-form-item label="提成余额">
                    <el-input v-model="sellerForm.saleFee" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="sellerForm.address"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="clearSeller">取 消</el-button>
                 <el-button type="primary" @click="addSeller">确 定</el-button>
            </span>
        </el-dialog>

        <el-dialog
                title="编辑销售"
                :visible.sync="dialogVisible1"
                width="30%">
            <el-form ref="form" :model="sellerForm" label-width="80px" >
                <el-form-item label="登录账号">
                    <el-input v-model="sellerForm.userName" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="销售名称">
                    <el-input v-model="sellerForm.nickName"></el-input>
                </el-form-item>
                <el-form-item label="销售电话">
                    <el-input v-model="sellerForm.mobile"></el-input>
                </el-form-item>
                <el-form-item label="性别">
                    <el-radio v-model="sellerForm.sex" :label="0">男</el-radio>
                    <el-radio v-model="sellerForm.sex" :label="1">女</el-radio>
                </el-form-item>
                <el-form-item label="年龄">
                    <el-input v-model="sellerForm.age"></el-input>
                </el-form-item>
                <el-form-item label="提成余额">
                    <el-input v-model="sellerForm.saleFee" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="sellerForm.address"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="clearSeller">取 消</el-button>
                 <el-button type="primary" @click="updateSeller">确 定</el-button>
            </span>
        </el-dialog>

        <el-dialog
                title="修改销售密码"
                :visible.sync="dialogVisible2"
                width="30%">
            <el-form ref="form" :model="sellerForm" label-width="100px" >
                <el-form-item label="销售旧密码">
                    <el-input v-model="sellerForm.oldPass"></el-input>
                </el-form-item>
                <el-form-item label="销售新密码">
                    <el-input v-model="sellerForm.newPass"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="clearSeller">取 消</el-button>
                 <el-button type="primary" @click="editSellerPass">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
import moment from 'moment'
    export default {
        name: "AdminList",
        data(){
            return{
                sellerList:[],
                sellerForm:{
                    id : 0,
                    userName : '',
                    mobile : '',
                    nickName : '',
                    sex : 0,
                    age : 0,
                    saleFee : 0,
                    address : '',
                    oldPass : '',
                    newPass : ''
                },
                dialogVisible:false,
                dialogVisible1:false,
                dialogVisible2:false,
                studentid:null,
                page:{
                    pageCurrent:0,
                    pageSize:5
                },
                total:0,
            }
        },
        methods:{
            //实现分页
            //每页记录数
            handleSizeChange:function(val){
                this.page.pageSize = val
                this.sellerSel()
            },
            //当前页
            handleCurrentChange:function(val){
                this.page.pageCurrent = val
                this.sellerSel()
            },

            //列表实现
            sellerSel:function () {
                let params = new FormData();
                params.append("pageCurrent", this.page.pageCurrent)
                params.append("pageSize", this.page.pageSize)
                
                this.$http.post(
                    '/admin/getSellerByPage',
                    params
                ).then((res)=>{
                    this.sellerList = res.data.result.pages.records
                    this.total =  res.data.result.pages.total;
                })
            },

            //逻辑删除
            delById:function (id) {
                let params = new FormData();
                params.append("id", id)
                
                this.$http.post(
                    '/admin/delSellerUser',
                    params
                ).then((res)=>{
                    if (res.data.success) {
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    }
                    this.sellerSel();
                })
            },

            //添加销售
            addSeller:function () {
                let params = new FormData();
                params.append("userName", this.sellerForm.userName)
                params.append("mobile", this.sellerForm.mobile)
                params.append("nickName", this.sellerForm.nickName)
                params.append("sex", this.sellerForm.sex)
                params.append("age", this.sellerForm.age)
                params.append("saleFee", this.sellerForm.saleFee)
                params.append("address", this.sellerForm.address)
                
                this.$http.post(
                    'admin/addSellerUser',
                    params
                ).then((res)=>{
                    if (res.data.success) {
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    }
                    else {
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                    this.clearSeller();
                })
            },

            //修改回显
            editById:function(id){
                this.dialogVisible1=true
                
                let params = new FormData();
                params.append("sellerId", id)
                
                this.$http.post(
                    '/admin/getSellerInfo',
                    params
                ).then((res)=>{
                    if (res.data.success) {
                        this.sellerForm = res.data.result.seller
                    }else{
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                })
            },

            //修改销售
            updateSeller:function(id){
                let params = new FormData();
                params.append("userId", this.sellerForm.id)
                params.append("nickName", this.sellerForm.nickName)
                params.append("mobile", this.sellerForm.mobile)
                params.append("sex", this.sellerForm.sex)
                params.append("age", this.sellerForm.age)
                params.append("saleFee", this.sellerForm.saleFee)
                params.append("address", this.sellerForm.address)
                
                this.$http.post(
                    '/admin/setSellerInfo',
                    params
                ).then((res)=>{
                    if (res.data.success) {
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    }else {
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                    this.clearSeller();
                })
            },

            //打开修改密码的dialog
            openEditSellerPass:function(id){
                this.sellerForm.id=id;
                this.dialogVisible2=true;

            },

            //修改密码
            editSellerPass:function(id){
                this.dialogVisible2=false
                let params = new FormData();
                params.append("userId", this.sellerForm.id)
                params.append("oldPass", this.sellerForm.oldPass)
                params.append("newPass", this.sellerForm.newPass)
                
                this.$http.post(
                    '/admin/setSellerPass',
                    params
                ).then((res)=>{
                    if (res.data.success) {
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    }
                    else {
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                    this.clearSeller()
                })
            },

            //清空表单
            clearSeller:function(){
                this.dialogVisible=false
                this.dialogVisible1=false
                this.dialogVisible2=false
                this.sellerForm.id=null

                this.sellerForm.userName=""
                this.sellerForm.mobile=""
                this.sellerForm.nickName=""
                this.sellerForm.sellerForm=0
                this.sellerForm.age=0
                this.sellerForm.saleFee=0
                this.sellerForm.address = ''
                this.sellerForm.oldPass = ''
                this.sellerForm.newPass = ''
                this.sellerSel()
            }

        },
        //过滤器
        filters:{
            statusFit:function (val) {
                if (val == '0'){
                    return "正常"

                }
                if (val == '1'){
                    return "离职"
                }
            },
            joinTimeFit:function (val) {
                return moment(val).format("YYYY-MM-DD hh:mm:ss")
            },
            sexFit:function (val) {
                if (val == '0'){
                    return "男"

                }
                if (val == '1'){
                    return "女"
                }
            }
        },
        mounted() {
            this.sellerSel();
        }
    }
</script>

<style scoped>

</style>
